package com.woniu.inlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.adapter.RepairAdapter;
import com.woniu.command.Command;
import com.woniu.command.repaircommand.AddRepairApplicationCommand;
import com.woniu.command.repaircommand.CheckCommand;
import com.woniu.command.repaircommand.ProductCommand;
import com.woniu.command.repaircommand.ProductedCommand;
import com.woniu.inlet.vo.RepairApplicationVo;
import com.woniu.outlet.po.RepairAcceptance;
import com.woniu.outlet.po.RepairApplication;
import com.woniu.service.RepairService;
import com.woniu.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "返修controller")
@RequestMapping("/Repair/")
public class RepairController {
    @Autowired
    private RepairService repairService;
    @Autowired
    private RepairAdapter repairAdapter;

    /** 添加返修申请
     * @param repairApplication
     * @return
     */
    @ApiOperation("添加返修申请")
    @PostMapping("addRepairApplication")
    @ApiOperationSupport(ignoreParameters = {"id","check","repairAcceptance","spare1","spare2","spare3"})
    public ResponseResult addRepairApplication(@RequestBody RepairApplication repairApplication){
        Command command = new AddRepairApplicationCommand();
        command.execute(repairApplication);
        return new ResponseResult(200,"添加返修申请成功");
    }

    /** 查询所有还没有审核申请的申请
     * @param orderCode
     * @return
     */
    @PreAuthorize("hasAnyAuthority('student:add')")
    @ApiOperation("查询所有没有审核申请")
    @GetMapping("getRepairApplicationList")
    public ResponseResult getRepairApplicationList(String orderCode){
        List<RepairApplication> repairApplicationList = repairService.getRepairApplicationList(orderCode);
        List<RepairApplicationVo> list = new ArrayList<>();
        for (RepairApplication repairApplication : repairApplicationList) {
            RepairApplicationVo repairApplicationVo = new RepairApplicationVo();
            BeanUtils.copyProperties(repairApplication,repairApplicationVo);
            list.add(repairApplicationVo);
        }
        return new ResponseResult(list,"查询所有没有审核申请成功",200);
    }

    /** 审核这个过程应该有两个结果,一个是审核通过,一个是审核不通过,当审核不通过时应该备注为什么不通过
     * @param id
     * @param flag
     * @return
     */
    @ApiOperation("审核")
    @PutMapping("updateStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "申请的id"),
            @ApiImplicitParam(name = "flag",value = "通过与不通过,true或false"),
            @ApiImplicitParam(name = "remarks",value = "说明,审核不通过需要说明理由"),
    })
    public ResponseResult updateStatus(Integer id,boolean flag,String remarks){
       repairAdapter.updateStatus(id,flag,remarks);
        return new ResponseResult(200,"查询所有没有审核申请成功");
    }

    /** 获取审核通过的返修列表
     * @param orderCode
     * @return
     */
    @ApiOperation("获取审核通过的")
    @GetMapping("getRepairList")
    public ResponseResult getRepairList(String orderCode){
        List<RepairApplication> repairList = repairService.getRepairList(orderCode);
        List<RepairApplicationVo> list = new ArrayList<>();
        for (RepairApplication repairApplication : repairList) {
            RepairApplicationVo repairApplicationVo = new RepairApplicationVo();
            BeanUtils.copyProperties(repairApplication,repairApplicationVo);
            list.add(repairApplicationVo);
        }
        return new ResponseResult(list,"查询所有没有审核申请成功",200);
    }


    /** 根据审核通过的返修申请去返修
     * @param id
     * @return
     */
    @PutMapping("product")
    @ApiOperation("返修")
    public ResponseResult product(Integer id){
        Command command = new ProductCommand();
        command.execute(id);
        return new ResponseResult(200,"返修");
    }


    /** 生产完成之后改修改状态为生产完成
     * @param id
     * @return
     */
    @ApiOperation("返修完成")
    @PutMapping("producted")
    public ResponseResult producted(Integer id){
        Command command = new ProductedCommand();
        command.execute(id);
        return new ResponseResult(200,"查询所有没有审核申请成功");
    }

    /** 查询所有生产完成之后的申请
     * @param orderCode
     * @return
     */
    @ApiOperation("查询所有生产完成之后的申请")
    @GetMapping("getProducted")
    public ResponseResult getProducted(String orderCode){
        List<RepairApplication> producted = repairService.getProducted(orderCode);
        List<RepairApplicationVo> list = new ArrayList<>();
        for (RepairApplication repairApplication : producted) {
            RepairApplicationVo repairApplicationVo = new RepairApplicationVo();
            BeanUtils.copyProperties(repairApplication,repairApplicationVo);
            list.add(repairApplicationVo);
        }
        return new ResponseResult(list,"查询所有生产完成之后的申请",200);
    }

    /** 生产完成之后验收
     * @param repairAcceptance
     * @return
     */
    @ApiOperation("返修完后验收")
    @PutMapping("check")
    @ApiOperationSupport(ignoreParameters = {"id","spare1","spare2","spare3"})
    public ResponseResult check(@RequestBody RepairAcceptance repairAcceptance){
        Command command = new CheckCommand();
        command.execute(repairAcceptance);
        return new ResponseResult(200,"查询所有没有审核申请成功");
    }

    /** 查询所有已验收的
     * @param orderCode
     * @return
     */
    @ApiOperation("查询所有已验收的")
    @GetMapping("getCheck")
    public ResponseResult getCheck(String orderCode){
        List<RepairApplication> check = repairService.getCheck(orderCode);
        List<RepairApplicationVo> list = new ArrayList<>();
        for (RepairApplication repairApplication : check) {
            RepairApplicationVo repairApplicationVo = new RepairApplicationVo();
            BeanUtils.copyProperties(repairApplication,repairApplicationVo);
            list.add(repairApplicationVo);
        }
        return new ResponseResult(list,"查询所有没有审核申请成功",200);
    }

    /** 根据id查看返修的详细信息,也就是返修申请表和返修验收表
     * @param id
     * @return
     */
    @GetMapping("getRepairDetail")
    @ApiOperation("查询返修详细信息")
    public ResponseResult getRepairDetail(Integer id){
        RepairApplication repairApplicationDetail = repairService.getRepairApplicationDetail(id);
        return new ResponseResult(repairApplicationDetail,"查询返修详细信息",200);
    }
}
